<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2020/04/21
 * Time: 16:39
 */
 
namespace HiCommon\Service;

use HiCommon\Repository\ShopAccountLogRepository;
use HiCommon\Repository\ShopAccountRepository;
use HiCommon\Repository\ShopRepository;

class ShopAccountService extends BaseService
{
    /*获取资金账户详情*/
    public function api_get_account_info($n_id = 0){
        $arr_option = [
          'with'=>['shop_account'],
          'field'=>['id','phone'],
        ];
        $result = ShopRepository::base_find_one($n_id,$arr_option);
        if(is_null($result)) {
            $result = [];
        }
        else{
            $result = $result->toarray();
        }
        return  hello_success('成功',$result);
    }

    /*修改商户资金账户信息*/
    public function api_edit_account_info($s_phone,$arr_input){
        $s_phone = str_trim($s_phone);
        $s_code = str_trim($arr_input['code']);
        $arr_input = array_trim($arr_input);
        unset($arr_input['code']);

        //验证验证码
        $s_real_code = redis_phone_code( $s_phone );
        if (empty($s_real_code)){
            return hello_error('验证码已过期');
        }
        if ($s_real_code != $s_code){
            return hello_error('验证码错误');
        }
        
        //修改资料
        $arr_where = ['id'=>$arr_input['id']];
        $result = ShopAccountRepository::base_update_or_create($arr_where,$arr_input)->toarray();
        if(empty($result)){
            return hello_error('保存失败',[],'修改资料失败');
        }

        //记录修改日志
        $arr_data = ['shop_id'=>$arr_input['id'],'json'=>json_encode($arr_input)];
        $result = ShopAccountLogRepository::base_add_one($arr_data)->toarray();
        if(empty($result)) {
            return hello_error('保存失败',[],'记录修改日志失败');
        }

        unset_redis_phone_code($s_phone);

        return  hello_success('保存成功',$result);
    }

}